package leetcode.Y2024M6.study;

import leetcode.Y2024M6.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class S114 {
    public void flatten(TreeNode root) {
       while (root!=null){
           if (root.left==null){
               root=root.right;
           }else {
               TreeNode pre=root.left;
               while (pre.right!=null){
                   pre=pre.right;
               }
               pre.right=root.right;
               root.right=root.left;
               root.left=null;
               root=root.right;
           }
       }
    }
    List<Integer> list=new ArrayList<>();
    public List<Integer> preView(TreeNode node){
        if (node==null) return null;
        list.add(node.val);
        preView(node.left);
        preView(node.right);
        return list;
    }

    @Test
    public void test() {
        int[] root={1,2,5,3,4,-1,6};
        TreeNode tree = TreeNode.createTree(root);
        System.out.println(preView(tree));
        flatten(tree);
    }
}
